package com.example.administrator.oriding.retrofitservice;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity
        implements BottomNavigationView.OnNavigationItemSelectedListener , ViewPager.OnPageChangeListener{

    //定义ViewPager
    private ViewPager viewPager;

    //定义BottomNavigationView
    private BottomNavigationView bottomNavigationView;

    //定义FragmentPagerAdapter适配器器
    private FragmentPagerAdapter adapter;

    //定义Fragment List，⽤用来保存4个Fragment
    private List<Fragment> fragmentList;


    @Override
    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home);

//获取界⾯面中的对象
        viewPager = findViewById(R.id.viewPager); bottomNavigationView =
                findViewById(R.id.bottomNavigationView);

//定义4个Fragment，并加⼊入List中
        fragmentList = new ArrayList<>();
        fragmentList.add(new ShopFragment());
        fragmentList.add(new CollectionFragment());
        fragmentList.add(new SearchFragment());
        fragmentList.add(new UserFragment());

//获取FragmentPagerAdapter适配器器
        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public Fragment getItem(int i) {

//根据位置返回对应的Fragment
//0对应ShopFragment， 1对应CollectionFragment。。。
                return fragmentList.get(i);
            }

            @Override
            public int getCount() {

//返回Fragment的数量量
                return fragmentList.size();
            }
        };

//添加适配器器
        viewPager.setAdapter(adapter);
//给ViewPager添加监听器器，当前类需要实现OnPageChangeListener接⼝口
        viewPager.addOnPageChangeListener(this);

//给BottomNavigationView添加监听器器，当前类需要实现OnNavigationItemSelectedListener接⼝口
        bottomNavigationView.setOnNavigationItemSelectedListener(this);

//选中"店铺"导航按钮
        bottomNavigationView.setSelectedItemId(R.id.nav_shop);
    }

    //监听BottomNavigationView
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { int id = 0;
//根据底部导航菜单项的id，指定ViewPager中的Fragment
        switch (menuItem.getItemId()){

            case R.id.nav_shop://
                id = 0; break;
            case R.id.nav_collection: id = 1;
                break;
            case R.id.nav_search: id = 2;
                break;
            case R.id.nav_user: id = 3;
                break;
        }

//指定ViewPager中的Fragment
        viewPager.setCurrentItem(id);
        return true;
    }

    //以下3个⽅方法监听ViewPager
    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {

        int id = 0; switch (i){

            case 0:
                id = R.id.nav_shop; break;
            case 1:
                id = R.id.nav_collection; break;
            case 2:
                id = R.id.nav_search; break;
            case 3:
                id = R.id.nav_user; break;
        }
        bottomNavigationView.setSelectedItemId(id);

    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}

